package com.mianle.userapi.util;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.apache.commons.codec.binary.Base64;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * 二维码
 *
 */
public class QrCodeUtils {

    /**
     * 生成二维码
     *
     * @param content 内容
     * @param width   宽度
     * @param height  高度
     * @throws WriterException
     * @throws IOException
     */
    public static BufferedImage getQRCode(String content, int width, int height) throws Exception {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //设置二维码边框宽度(0-4)
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);
        return image;
    }

    /**
     * 生成二维码（返回对应的输出流）
     *
     * @param content 内容
     * @param width   宽度
     * @param height  高度
     * @throws WriterException
     * @throws IOException
     */
    public static ByteArrayOutputStream getQRCodeOutputStream(String content, int width, int height) throws Exception {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //设置二维码边框宽度(0-4)
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        String format = "png";
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(matrix, format, stream);
        return stream;
    }

    /**
     * 往输出流中输出
     *
     * @param content
     * @param width
     * @param height
     * @param out
     * @throws Exception
     */
    public static void pushToOutputStream(String content, int width, int height, OutputStream out) throws Exception {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //设置二维码边框宽度(0-4)
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        String format = "png";
        MatrixToImageWriter.writeToStream(matrix, format, out);
    }

    /**
     * 生成二维码（返回对应的base64图片）
     *
     * @param content 内容
     * @param width   宽度
     * @param height  高度
     * @throws WriterException
     * @throws IOException
     */
    public static String getQRCodeBase64(String content, int width, int height) throws Exception {
        ByteArrayOutputStream buf = getQRCodeOutputStream(content, width, height);
        String png = Base64.encodeBase64String(buf.toByteArray());
        buf.close();
        return png;
    }

    /**
     * 生成二维码
     *
     * @param content 内容
     * @param width   宽度
     * @param height  高度
     * @throws WriterException
     * @throws IOException
     */
    public static void getQRCodeFile(String content, int width, int height) throws WriterException, IOException {

        String format = "jpg";// 图像类型  
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //设置二维码边框宽度(0-4)
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵  

        Path path = new File("E:\\aa.png").toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像  
    }

    public static void main(String[] args){
        String content = "http://www.sydz35.com?rate=1&type=";
        String png;
		try {
			png = getQRCodeBase64(content, 600, 600);
			System.out.println(png);
		} catch (Exception e) {
		}
    }

}
